(function () {
    "use strict";

    angular.module("core").directive('defaultSrc', directive);

    function directive() {
        return {
            link: function (scope, element, attrs) {

                //[ trata imagens em branco ]
                scope.$watch(function () {
                    return attrs['ngSrc'];
                }, function (value) {
                    if (!value) {
                        element.attr('src', attrs.defaultSrc);
                    }
                });

                //[ trata imagens não encontradas ]
                element.bind('error', function () {
                    if (attrs.src !== attrs.defaultSrc) {
                        attrs.$set('src', attrs.defaultSrc);
                    }
                });
            }
        }
    }

})();

(function () {
    "use strict";

    angular.module("core").directive('stringToNumber', directive);

    function directive() {
        return {
            require: 'ngModel',
            link: function (scope, element, attrs, ngModel) {
                ngModel.$parsers.push(function (value) {
                    return '' + value;
                });
                ngModel.$formatters.push(function (value) {
                    return parseFloat(value, 10);
                });
            }
        };
    }

})();

(function () {
    "use strict";

    angular.module("core").directive('ngFiles', directive);

    directive.$inject = ['$parse'];


    function directive($parse) {
        function fn_link(scope, element, attrs) {

            var onChange = $parse(attrs.ngFiles);
            element.on('change', function (event) {
                onChange(scope, { $files: event.target.files });
            });
        };

        return {
            link: fn_link
        }
    }

})();

(function () {
    "use strict";

    angular.module("core").directive('backgroundImage', directive);

    function directive() {
        return {
            restrict: 'A',
            scope: {
                backgroundImage: '@',
                defaultBackgroundImage: '@',
                textProtection: '@',
            },
            link: function (scope, element, attrs) {
                //[ trata imagens em branco ]
                scope.$watch(function () {
                    if (scope.backgroundImage) {
                        if (angular.isDefined(scope.textProtection))
                            element.css({ 'background-image': 'linear-gradient(rgba(0,0,0,.5), rgba(0,0,0,0)), url(' + scope.backgroundImage + ')' });
                        else
                            element.css({ 'background-image': 'url(' + scope.backgroundImage + ')' });
                    }
                }, function (value) {
                    if (!value) {
                        if (angular.isDefined(scope.textProtection))
                            element.css({ 'background-image': 'linear-gradient(rgba(0,0,0,.5), rgba(0,0,0,0)), url(' + scope.defaultBackgroundImage + ')' });
                        else
                            element.css({ 'background-image': 'url(' + scope.defaultBackgroundImage + ')' });
                    }
                });

                //[ trata imagens não encontradas ]
                element.parent.bind('error', function () {
                    if (angular.isDefined(scope.textProtection))
                        element.css({ 'background-image': 'linear-gradient(rgba(0,0,0,.5), rgba(0,0,0,0)), url(' + scope.defaultBackgroundImage + ')' });
                    else
                        element.css({ 'background-image': 'url(' + scope.defaultBackgroundImage + ')' });
                });

            }
        }
    }

})();

(function () {
    "use strict";

    angular.module("core").directive('trackBy', directive);

    function directive() {
        return {
            restrict: 'A',
            scope: {
                ngModel: '=',
                ngValue: '=',
                trackBy: '@',
            },
            link: function (val) {
                if (val.ngValue[val.trackBy] === val.ngModel[val.trackBy]) {
                    val.ngModel = val.ngValue;
                }
            }
        };
    }

})();

(function () {
    "use strict";

    angular.module('core').directive("formatDate", directive);

    function directive() {
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function (scope, elem, attr, modelCtrl) {
                modelCtrl.$formatters.push(function (modelValue) { return modelValue ? new Date(modelValue) : null; });
            }
        };
    }

})();

(function () {
    "use strict";

    angular.module("core").directive('coverImage', directive);

    function directive() {
        return {
            link: function (scope, element, attrs) {
                element.css({ 'background-image': 'linear-gradient(rgba(0,0,0,.7), rgba(0,0,0,.5)), url(' + scope.config.data.UrlCapa + '?v=' + scope.cacheVersion.get() + ')' });
            }
        }
    }

})();

(function () {
    "use strict";

    angular.module("core").directive('authorizationRequired', directive);
    directive.$inject = ['$timeout'];

    function directive($timeout) {
        return {
            restrict: 'A',
            scope: {
                ngClick: '&',
                authorizationRequired: '@',
            },
            link: function (scope, element, attrs) {
                if (angular.isDefined(eval(scope.authorizationRequired)) && !eval(scope.authorizationRequired))
                    return;

                element.unbind("click").bind("click", function ($event) {
                    $event.preventDefault();

                    $timeout(function () {
                        scope.autorizacaoGerente.ativo = true;
                        scope.autorizacaoGerente.callback = scope.ngClick;
                    });
                });

            }
        }
    }

})();

(function () {
    "use strict";

    angular.module("core").directive('focus', directive);
    directive.$inject = ['$timeout'];

    function directive($timeout) {
        return {
            restrict: 'A',
            scope: {
                trigger: '@focus'
            },
            link: function (scope, element) {
                scope.$watch('trigger', function (value) {
                    if (value !== 'false') {
                        $timeout(function () {
                            element[0].focus();
                        });
                    }
                });
            }
        };
    }

})();

(function () {
    "use strict";

    angular.module("core").directive('nivelAvaliacao', directive);
    directive.$inject = ['toolsService'];

    function directive(toolsService) {
        return {
            restrict: 'EA',
            scope: {
                stars: "=",
                inverse: "=",
            },
            template:
                '<div>' +
                '   <label class="nivel-avaliacao text-nowrap" ng-class="{ inverse: inverse }" ng-if="stars" title="Nível de avaliação dos consumidores">' +
                '       <i ng-repeat="star in getStars(stars) track by $index" class="material-icons">{{star}}</i>' +
                '   </label>' +
                '   <label class="nivel-avaliacao text-nowrap nulo"  ng-class="{ inverse: inverse }" ng-if="!stars" title="Nenhuma avaliação feita">' +
                '       <i class="material-icons">star_borderstar_borderstar_borderstar_borderstar_border</i>' +
                '   </label>' +
                '</div>',
            replace: true,
            link: function ($scope, element, attrs) {
                $scope.getStars = function (count) {
                    var list = [];
                    for (var i = 0; i < count; i++)
                        list.push("star");
                    for (var i = count; i < 5; i++)
                        list.push("star_border");
                    return list;
                };
            }
        };
    }

})();

(function () {
    "use strict";

    angular.module("core").directive('dateIcon', directive);

    function directive() {
        return {
            restrict: 'EA',
            scope: {
                value: "=",
                time: "="
            },
            template:
                '<div class="date-icon" >' +
                '    <div class="left">' +
                '        <div class="day">{{ value | date: "dd"}}</div>' +
                '        <div class="month">{{ value | date: "MMM"}}</div>' +
                '    </div>' +
                '    <div class="right">' +
                '        <div class="year">{{ value | date: "yyyy"}}</div>' +
                '        <div class="time" ng-if="time">{{ value | date: "HH:mm"}}</div>' +
                '    </div>' +
                '</div >',

            replace: true,
            link: function ($scope, element, attrs) { }
        };
    }

})();

(function () {
    "use strict";

    angular.module("core").directive('indicadorPassos', directive);

    function directive() {
        return {
            restrict: 'EA',
            scope: {
                count: "=",
                index: "="
            },
            template:
                '<ul class="guide">' +
                '   <li ng-class="{ current: index !== $index }" ng-repeat="passo in getIndicadorPassos(count)" space-between>{{passo}}</li> ' +
                '</ul>',
            replace: true,
            link: function ($scope, element, attrs) {
                $scope.getIndicadorPassos = function (count) {
                    var list = [];
                    for (var i = 0; i < count; i++)
                        list.push(i + 1);
                    return list;
                };
            }

        };
    }

})();

(function () {
    "use strict";

    angular.module("core").directive('spaceBetween', directive);

    function directive() {
        return function (scope, element) {
            if (!scope.$last) {
                element.after('&#32;');
            }
        };
    }

})();

(function () {
    "use strict";

    angular.module("core").directive('requerido', directive);

    function directive() {
        return {
            restrict: 'EA',
            scope: {
                stars: "=",
                inverse: "="
            },
            template:
                '<i class="text-primary">*</i>',
            replace: true
        };
    }

})();
